//
//  CloudReserveDateController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/27.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class CloudReserveDateController: UIViewController {
    
    var closeClosure: ((_ vm: CloudReserveDateListViewModel) -> Void)?
    
    private let detailVm: DoctorDetailViewModel
    private let scheduleVm: DoctorScheduleViewModel
    
    init(with detailVm: DoctorDetailViewModel, scheduleVm: DoctorScheduleViewModel) {
        self.detailVm = detailVm
        self.scheduleVm = scheduleVm
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
    }
    
    private var viewModel: CloudReserveDateViewModel? {
        didSet {
            collectionView.reloadData()
        }
    }
    
    private var selectedIndex: IndexPath?
    
    @IBOutlet private weak var contentView: UIView! {
        didSet {
            contentView.layer.cornerRadius = 16
            contentView.layer.masksToBounds = true
        }
    }

    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.delaysContentTouches = false
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.contentInset = UIEdgeInsets(top: 0, left: 32, bottom: 0, right: 32)
            collectionView.register(CloudReserveDateCell.nibForCell(),
                                    forCellWithReuseIdentifier: CloudReserveDateCell.reuseIdentifier())
        }
    }
    
    @IBOutlet private weak var activityView: UIActivityIndicatorView!
    
    @IBOutlet private weak var closeBtn: UIButton! {
        didSet {
            closeBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
        }
    }
    
    @IBAction private func closeAction(_ sender: UIButton) {
        view.backgroundColor = .clear
        dismiss(animated: true)
    }
    
    private func loadData() {
        activityView.startAnimating()
        CloudReserveDateViewModel.getReserveDateList(doctorId: "\(detailVm.doctorId)",
                                                     departmentId: scheduleVm.departmentId,
                                                     scheduleDate: scheduleVm.scheduleDate,
                                                     timeInterval: scheduleVm.timeInterval,
                                                     success: { model in
            self.activityView.stopAnimating()
            self.viewModel = CloudReserveDateViewModel(model: model)
        }) { errorMsg in
            self.activityView.stopAnimating()
            HUDTool.showError(message: errorMsg)
        }
    }
}

extension CloudReserveDateController:
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.numberOfItems ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CloudReserveDateCell.reuseIdentifier(),
                                                      for: indexPath) as! CloudReserveDateCell
        if let vm = viewModel?.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        let cell = collectionView.cellForItem(at: indexPath) as? CloudReserveDateCell
        cell?.setSelected(reset: false)
        
        view.backgroundColor = .clear
        dismiss(animated: true) {
            guard let vm = self.viewModel?.viewModel(for: indexPath) else { return }
            self.closeClosure?(vm)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return viewModel?.itemSize ?? .zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 20
    }
}
